home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import os
- import rhythmdb
- import gnomevfs
- import rb
- import gobject
- IMAGE_NAMES = [
- 'cover',
- 'album',
- 'albumart',
- '.folder',
- 'folder']
- LOAD_DIRECTORY_FLAGS = gnomevfs.FILE_INFO_GET_MIME_TYPE | gnomevfs.FILE_INFO_FORCE_FAST_MIME_TYPE | gnomevfs.FILE_INFO_FOLLOW_LINKS
- ITEMS_PER_NOTIFICATION = 10
- ART_SAVE_NAME = 'Cover.jpg'
- ART_SAVE_FORMAT = 'jpeg'
- ART_SAVE_SETTINGS = {
- 'quality': '100' }
-
- def file_root(f_name):
- return os.path.splitext(f_name)[0].lower()
-
-
- def shared_prefix_length(a, b):
- l = 0
- while a[l] == b[l]:
- l = l + 1
- return l
-
-
- class LocalCoverArtSearch:
-
- def __init__(self):
- pass
-
-
- def _load_dir_cb(self, handle, files, exception, .4):
- (results, on_search_completed_cb, entry, args) = .4
- for f in files:
- if f.mime_type is None:
- continue
- if f.mime_type.startswith('image/') and f.permissions & gnomevfs.PERM_USER_READ:
- results.append(f.name)
- continue
-
- if exception:
- on_search_completed_cb(self, entry, results, *args)
- if not issubclass(exception, gnomevfs.EOFError):
- print 'Error reading "%s": %s' % (self.uri.parent, exception)
-
-
-
-
- def search(self, db, entry, on_search_completed_cb, *args):
- self.uri = None
-
- try:
- self.uri = gnomevfs.URI(entry.get_playback_uri())
- except TypeError:
- pass
-
- if self.uri is None or self.uri.scheme == 'http':
- print 'not searching for local art for %s' % self.uri
- on_search_completed_cb(self, entry, [], *args)
- return None
- self.artist = db.entry_get(entry, rhythmdb.PROP_ARTIST)
- self.album = db.entry_get(entry, rhythmdb.PROP_ALBUM)
- print 'searching for local art for %s' % self.uri
- gnomevfs.async.load_directory(self.uri.parent, self._load_dir_cb, LOAD_DIRECTORY_FLAGS, ITEMS_PER_NOTIFICATION, data = ([], on_search_completed_cb, entry, args))
-
-
- def search_next(self):
- return False
-
-
- def get_best_match_urls(self, results):
- for name in [
- file_root(self.uri.short_name)] + IMAGE_NAMES:
- for f_name in results:
- if file_root(f_name) == name:
- yield self.uri.parent.append_file_name(f_name)
- continue
-
-
- artist = self.artist.lower()
- album = self.album.lower()
- for f_name in results:
- f_root = file_root(f_name).lower()
- if f_root.find(artist) != -1 and f_root.find(album) != -1:
- yield self.uri.parent.append_file_name(f_name).path
- continue
-
- match = (2, None)
- for f_name in results:
- pl = shared_prefix_length(f_name, self.uri.short_name)
- if pl > match[0]:
- match = (pl, f_name)
- continue
-
- if match[1] is not None:
- yield self.uri.parent.append_file_name(match[1]).path
-
-
-
- def pixbuf_save(self, plexer, pixbuf, uri):
- gnomevfs.async.create(uri, plexer.send(), gnomevfs.OPEN_WRITE | gnomevfs.OPEN_TRUNCATE, False, 420, gnomevfs.PRIORITY_DEFAULT)
- yield None
- (handle, result) = (_,)
- if result:
- print 'Error creating "%s": %s' % (uri, result)
- return None
-
- def pixbuf_cb(buf):
- data = [
- buf]
- status = []
-
- def write_coro(w_plexer):
- while data:
- buf = data.pop()
- handle.write(buf, w_plexer.send())
- yield None
- (_, requested, result, written) = (_,)
- if result:
- print 'Error writing "%s": %s' % (uri, result)
- status.insert(0, False)
- return None
- if written < requested:
- data.insert(0, buf[written:])
- continue
- result
- continue
- w_plexer.receive()
- status.insert(0, True)
-
- rb.Coroutine(write_coro).begin()
- while not status:
- gobject.main_context_default().iteration()
- continue
- (None, None, None, None)
- return status[0]
-
- pixbuf.save_to_callback(pixbuf_cb, ART_SAVE_FORMAT, ART_SAVE_SETTINGS)
- handle.close(plexer.send())
- yield None
- (plexer.receive(), result)
- (_, result) = (_,)
-
-
- def _save_dir_cb(self, handle, files, exception, .4):
- (db, entry, pixbuf) = .4
- (artist, album) = [ db.entry_get(entry, x) for x in [
- rhythmdb.PROP_ARTIST,
- rhythmdb.PROP_ALBUM] ]
- for f in files:
- uri = str(self.uri.parent.append_file_name(f.name))
- u_entry = db.entry_lookup_by_location(uri)
- print 'Not saving local art; encountered unknown file (%s)' % uri
- handle.cancel()
- return None
-
-
-
- def save_pixbuf(self, db, entry, pixbuf):
- self.uri = None
-
- try:
- self.uri = gnomevfs.URI(entry.get_playback_uri())
- except TypeError:
- pass
-
- if self.uri is None or self.uri.scheme == 'http':
- print 'not saving local art for %s' % self.uri
- return None
- print 'checking whether to save local art for %s' % self.uri
- gnomevfs.async.load_directory(self.uri.parent, self._save_dir_cb, LOAD_DIRECTORY_FLAGS, ITEMS_PER_NOTIFICATION, data = (db, entry, pixbuf))
-
-
-